package com.example.cardgame

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.GridLayout
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.children

const val FIRST_MSG = "FIRST_MSG"
const val SECOND_MSG = "SECOND_MSG"
class MainActivity : AppCompatActivity(){
    var score=-999999;
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val vname = findViewById<TextView>(R.id.vname)
        val vscore = findViewById<TextView>(R.id.vscore)
        val start = findViewById<Button>(R.id.start)
        val change = findViewById<Button>(R.id.change)
        vname.text = "用户12032"
        vscore.text = "0"

        start.setOnClickListener(){
            val intent = Intent(this, GameActivity::class.java)
            startActivityForResult(intent, 0)
        }

        change.setOnClickListener(){
            val intent = Intent(this, ChangeActivity::class.java)
            startActivityForResult(intent, 0)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == 0){
            if(resultCode == 1){
                val num = data?.getStringExtra("SECOND_MSG")!!.toInt()
                val vscore = findViewById<TextView>(R.id.vscore)
                if (score<num){
                    vscore.text=num.toString()
                }
                score=num
            }
            else if(resultCode == 2){
                val str = data?.getStringExtra("FIRST_MSG")?.split(",")
                str?.get(0)?.length?.let {
                    if(it > 0){
                        val vname = findViewById<TextView>(R.id.vname)
                        vname.text = str[0]
                    }
                }
                str?.get(1)?.length?.let {
                    if(it > 0){
                        val vage = findViewById<TextView>(R.id.vage)
                        vage.text = str[1]
                    }
                }
                str?.get(2)?.length?.let {
                    if(it > 0){
                        val vsex = findViewById<TextView>(R.id.vsex)
                        vsex.text = str[2]
                    }
                }
                str?.get(3)?.length?.let {
                    if(it > 0){
                        val vnum = findViewById<TextView>(R.id.vnum)
                        vnum.text = str[3]
                    }
                }
            }
        }
    }

}